﻿namespace BaseClass.Duoc
{
    using ConfigDatabase.Class;
    using Npgsql;
    using NpgsqlTypes;
    using System;
    using System.Data;

    public class dtoDXuatBanLe
    {
        private ConnectDatabase _condata;
        private NpgsqlConnection _connect;
        private decimal _dechietkhau;
        private long _deid;
        private int _deiddangnhap;
        private long _deidttvp;
        private long _desophieu;
        private decimal _detienkhachdua;
        private decimal _detongtien;
        private int _iidgioitinh;
        private int _iidloaixuat;
        private int _iidnhomkhachhang;
        private int _isolanin;
        private string _sdiachi;
        private string _sghichu;
        private string _shoten;
        private string _skihieu;
        private string _snamsinh;
        private string _sngay;
        private string _sngaycapnhat;
        private string _sSchema;
        private string _stenmaytinh;

        public dtoDXuatBanLe()
        {
            this.KhoiTao();
            this._condata = new ConnectDatabase();
            this._sSchema = this._condata.Schema;
        }

        public dtoDXuatBanLe(ConnectDatabase conn)
        {
            this.KhoiTao();
            this._condata = conn;
            this._sSchema = this._condata.Schema;
        }

        public bool Delete()
        {
            bool flag;
            try
            {
                this._connect = new NpgsqlConnection(this._condata.StringConnect);
                this._connect.Open();
                flag = this.Delete(this._connect);
            }
            catch (NpgsqlException exception)
            {
                ConnectDatabase.TrackingError(exception.ToString(), "Delete()", "dtoDXuatBanLe.cs");
                flag = false;
            }
            finally
            {
                this._connect.Close();
                this._connect.Dispose();
            }
            return flag;
        }

        public bool Delete(NpgsqlConnection connect)
        {
            try
            {
                NpgsqlCommand command = new NpgsqlCommand(string.Concat(new object[] { "delete from ", this._sSchema, ".", tableName, " where 1=1 and ", columnID, " = ", this._deid }), connect) {
                    CommandType = CommandType.Text
                };
                return (command.ExecuteNonQuery() != 0);
            }
            catch (NpgsqlException exception)
            {
                ConnectDatabase.TrackingError(exception.ToString(), "Delete()", "dtoDXuatBanLe.cs");
                return false;
            }
        }

        public int GetIDInsert()
        {
            int iDInsert;
            try
            {
                this._connect = new NpgsqlConnection(this._condata.StringConnect);
                this._connect.Open();
                iDInsert = this.GetIDInsert(this._connect);
            }
            catch (NpgsqlException exception)
            {
                ConnectDatabase.TrackingError(exception.ToString(), "GetIDInsert()", "dtoDXuatBanLe.cs");
                iDInsert = 1;
            }
            finally
            {
                this._connect.Close();
                this._connect.Dispose();
            }
            return iDInsert;
        }

        public int GetIDInsert(NpgsqlConnection connect)
        {
            try
            {
                DataTable dataTable = new DataTable();
                new NpgsqlDataAdapter(" select max(id) as idmax  from " + this._sSchema + "." + tableName + " ", connect).Fill(dataTable);
                int num = 0;
                try
                {
                    num = Convert.ToInt32(dataTable.Rows[0][0].ToString());
                }
                catch
                {
                }
                return ++num;
            }
            catch (NpgsqlException exception)
            {
                ConnectDatabase.TrackingError(exception.ToString(), "GetIDInsert(NpgsqlConnection connect)", "dtoDXuatBanLe.cs");
                return 1;
            }
        }

        public bool Insert()
        {
            bool flag;
            try
            {
                this._connect = new NpgsqlConnection(this._condata.StringConnect);
                this._connect.Open();
                flag = this.Insert(this._connect);
            }
            catch (NpgsqlException exception)
            {
                ConnectDatabase.TrackingError(exception.ToString(), "Insert()", "dtoDXuatBanLe.cs");
                flag = false;
            }
            finally
            {
                this._connect.Close();
                this._connect.Dispose();
            }
            return flag;
        }

        public bool Insert(NpgsqlConnection connect)
        {
            try
            {
                NpgsqlCommand command = new NpgsqlCommand(string.Concat(new object[] { 
                    "insert into ", this._sSchema, ".", tableName, "(", columnID, ",", columnSOPHIEU, ",", columnNGAY, ",", columnTONGTIEN, ",", columnHOTEN, ",", columnIDGIOITINH, 
                    ",", columnNAMSINH, ",", columnDIACHI, ",", columnIDLOAIXUAT, ",", columnGHICHU, ",", columnTENMAYTINH, ",", columnIDDANGNHAP, ",", columnCHIETKHAU, ",", columnIDNHOMKHACHHANG, 
                    ",", columnSOLANIN, ",", columnKIHIEU, ",", columnIDTTVP, ",", columnTIENKHACHDUA, ") values(", this._deid, ",", this._desophieu, ",to_timestamp('", this._sngay, "','dd/mm/yyyy hh24:mi'),:", columnTONGTIEN, 
                    ",:", columnHOTEN, ",", this._iidgioitinh, ",'", this._snamsinh, "',:", columnDIACHI, ",", this._iidloaixuat, ",:", columnGHICHU, ",:", columnTENMAYTINH, ",", this._deiddangnhap, 
                    ",:", columnCHIETKHAU, ",", this._iidnhomkhachhang, ",", this._isolanin, ",'", this._skihieu, "',", this._deidttvp, ",:", columnTIENKHACHDUA, ")"
                 }), connect) {
                    CommandType = CommandType.Text
                };
                command.Parameters.Add(columnHOTEN, NpgsqlDbType.Text, 200).Value = this._shoten;
                command.Parameters.Add(columnDIACHI, NpgsqlDbType.Text, 200).Value = this._sdiachi;
                command.Parameters.Add(columnGHICHU, NpgsqlDbType.Text, 200).Value = this._sghichu;
                command.Parameters.Add(columnTENMAYTINH, NpgsqlDbType.Text, 200).Value = this._stenmaytinh;
                command.Parameters.Add(columnCHIETKHAU, NpgsqlDbType.Numeric, 20).Value = this._dechietkhau;
                command.Parameters.Add(columnTONGTIEN, NpgsqlDbType.Numeric, 20).Value = this._detongtien;
                command.Parameters.Add(columnTIENKHACHDUA, NpgsqlDbType.Numeric, 20).Value = this._detienkhachdua;
                return (command.ExecuteNonQuery() != 0);
            }
            catch (NpgsqlException exception)
            {
                ConnectDatabase.TrackingError(exception.ToString(), "Insert()", "dtoDXuatBanLe.cs");
                return false;
            }
        }

        private void KhoiTao()
        {
            this._deid = 0L;
            this._desophieu = 0L;
            this._sngay = "";
            this._detongtien = 0M;
            this._shoten = "";
            this._iidgioitinh = 0;
            this._snamsinh = "";
            this._sdiachi = "";
            this._iidloaixuat = 0;
            this._sghichu = "";
            this._stenmaytinh = "";
            this._sngaycapnhat = "";
            this._deiddangnhap = 0;
            this._dechietkhau = 0M;
            this._iidnhomkhachhang = 0;
            this._isolanin = 0;
            this._skihieu = "";
            this._deidttvp = 0L;
            this._detienkhachdua = 0M;
        }

        public void Load()
        {
            try
            {
                this._connect = new NpgsqlConnection(this._condata.StringConnect);
                this._connect.Open();
                this.Load(this._connect);
            }
            catch (NpgsqlException exception)
            {
                ConnectDatabase.TrackingError(exception.ToString(), "Load()", "dtoDXuatBanLe.cs");
            }
            finally
            {
                this._connect.Close();
                this._connect.Dispose();
            }
        }

        public void Load(NpgsqlConnection connect)
        {
            try
            {
                object obj2;
                string str = "";
                if (this._deid > 0L)
                {
                    obj2 = str;
                    str = string.Concat(new object[] { obj2, " and ", columnID, "=", this._deid });
                }
                if (this._deidttvp > 0L)
                {
                    obj2 = str;
                    str = string.Concat(new object[] { obj2, " and ", columnIDTTVP, "=", this._deidttvp });
                }
                DataTable dataTable = new DataTable();
                new NpgsqlDataAdapter(" select  " + columnID + "," + columnSOPHIEU + ",to_char(" + columnNGAY + ",'dd/mm/yyyy hh24:mi') as " + columnNGAY + "," + columnTONGTIEN + "," + columnHOTEN + "," + columnIDGIOITINH + "," + columnNAMSINH + "," + columnDIACHI + "," + columnIDLOAIXUAT + "," + columnGHICHU + "," + columnTENMAYTINH + ",to_char(" + columnNGAYCAPNHAT + ",'dd/mm/yyyy hh24:mi:ss') as " + columnNGAYCAPNHAT + "," + columnIDDANGNHAP + "," + columnCHIETKHAU + "," + columnIDNHOMKHACHHANG + "," + columnSOLANIN + "," + columnKIHIEU + "," + columnIDTTVP + "," + columnTIENKHACHDUA + " from " + this._sSchema + "." + tableName + " where " + ((str != "") ? (" 1=1 " + str) : " 1=0"), connect).Fill(dataTable);
                if (dataTable.Rows.Count != 0)
                {
                    this._deid = Convert.ToInt64(dataTable.Rows[0][columnID].ToString());
                    this._desophieu = Convert.ToInt64(dataTable.Rows[0][columnSOPHIEU].ToString());
                    this._sngay = dataTable.Rows[0][columnNGAY].ToString();
                    this._detongtien = Convert.ToDecimal(dataTable.Rows[0][columnTONGTIEN].ToString());
                    this._shoten = dataTable.Rows[0][columnHOTEN].ToString();
                    this._iidgioitinh = Convert.ToInt32(dataTable.Rows[0][columnIDGIOITINH].ToString());
                    this._snamsinh = dataTable.Rows[0][columnNAMSINH].ToString();
                    this._sdiachi = dataTable.Rows[0][columnDIACHI].ToString();
                    this._iidloaixuat = Convert.ToInt32(dataTable.Rows[0][columnIDLOAIXUAT].ToString());
                    this._sghichu = dataTable.Rows[0][columnGHICHU].ToString();
                    this._stenmaytinh = dataTable.Rows[0][columnTENMAYTINH].ToString();
                    this._sngaycapnhat = dataTable.Rows[0][columnNGAYCAPNHAT].ToString();
                    this._deiddangnhap = Convert.ToInt32(dataTable.Rows[0][columnIDDANGNHAP].ToString());
                    this._dechietkhau = Convert.ToDecimal(dataTable.Rows[0][columnCHIETKHAU].ToString());
                    this._iidnhomkhachhang = Convert.ToInt32(dataTable.Rows[0][columnIDNHOMKHACHHANG].ToString());
                    this._isolanin = Convert.ToInt32(dataTable.Rows[0][columnSOLANIN].ToString());
                    this._skihieu = dataTable.Rows[0][columnKIHIEU].ToString();
                    this._deidttvp = Convert.ToInt64(dataTable.Rows[0][columnIDTTVP].ToString());
                    this._detienkhachdua = Convert.ToDecimal(dataTable.Rows[0][columnTIENKHACHDUA].ToString());
                }
            }
            catch (NpgsqlException exception)
            {
                ConnectDatabase.TrackingError(exception.ToString(), "Load(NpgsqlConnection connect)", "dtoDXuatBanLe.cs");
            }
        }

        public DataTable LoadAll()
        {
            DataTable table;
            try
            {
                this._connect = new NpgsqlConnection(this._condata.StringConnect);
                this._connect.Open();
                table = this.LoadAll(this._connect);
            }
            catch (NpgsqlException exception)
            {
                ConnectDatabase.TrackingError(exception.ToString(), "LoadAll()", "dtoDXuatBanLe.cs");
                table = null;
            }
            finally
            {
                this._connect.Close();
                this._connect.Dispose();
            }
            return table;
        }

        public DataTable LoadAll(NpgsqlConnection connect)
        {
            try
            {
                DataTable dataTable = new DataTable();
                new NpgsqlDataAdapter(" select  " + columnID + "," + columnSOPHIEU + ",to_char(" + columnNGAY + ",'dd/mm/yyyy hh24:mi') as " + columnNGAY + "," + columnTONGTIEN + "," + columnHOTEN + "," + columnIDGIOITINH + "," + columnNAMSINH + "," + columnDIACHI + "," + columnIDLOAIXUAT + "," + columnGHICHU + "," + columnTENMAYTINH + "," + columnIDDANGNHAP + "," + columnCHIETKHAU + "," + columnIDNHOMKHACHHANG + "," + columnSOLANIN + "," + columnKIHIEU + "," + columnIDTTVP + "," + columnTIENKHACHDUA + " from " + this._sSchema + "." + tableName + " ", connect).Fill(dataTable);
                return dataTable;
            }
            catch (NpgsqlException exception)
            {
                ConnectDatabase.TrackingError(exception.ToString(), "LoadAll(NpgsqlConnection connect)", "dtoDXuatBanLe.cs");
                return null;
            }
        }

        public bool Update()
        {
            bool flag;
            try
            {
                this._connect = new NpgsqlConnection(this._condata.StringConnect);
                this._connect.Open();
                flag = this.Update(this._connect);
            }
            catch (NpgsqlException exception)
            {
                ConnectDatabase.TrackingError(exception.ToString(), "Update()", "dtoDXuatBanLe.cs");
                flag = false;
            }
            finally
            {
                this._connect.Close();
                this._connect.Dispose();
            }
            return flag;
        }

        public bool Update(NpgsqlConnection connect)
        {
            try
            {
                NpgsqlCommand command = new NpgsqlCommand(string.Concat(new object[] { 
                    "update ", this._sSchema, ".", tableName, " set ", columnSOPHIEU, " = ", this._desophieu, ",", columnNGAY, " = to_timestamp('", this._sngay, "','dd/mm/yyyy hh24:mi'),", columnTONGTIEN, " = :", columnTONGTIEN, 
                    ",", columnHOTEN, " = :", columnHOTEN, ",", columnIDGIOITINH, " = ", this._iidgioitinh, ",", columnNAMSINH, " = '", this._snamsinh, "',", columnDIACHI, " = :", columnDIACHI, 
                    ",", columnIDLOAIXUAT, " = ", this._iidloaixuat, ",", columnGHICHU, " = :", columnGHICHU, ",", columnTENMAYTINH, " = :", columnTENMAYTINH, ",", columnIDDANGNHAP, " = ", this._deiddangnhap, 
                    ",", columnCHIETKHAU, "=:", columnCHIETKHAU, ",", columnIDNHOMKHACHHANG, "=", this._iidnhomkhachhang, ",", columnSOLANIN, "=", this._isolanin, ",", columnKIHIEU, "='", this._skihieu, 
                    "',", columnIDTTVP, "=", this._deidttvp, ",", columnTIENKHACHDUA, "=:", columnTIENKHACHDUA, " where ", columnID, " = ", this._deid
                 }), connect) {
                    CommandType = CommandType.Text
                };
                command.Parameters.Add(columnHOTEN, NpgsqlDbType.Text, 200).Value = this._shoten;
                command.Parameters.Add(columnDIACHI, NpgsqlDbType.Text, 200).Value = this._sdiachi;
                command.Parameters.Add(columnGHICHU, NpgsqlDbType.Text, 200).Value = this._sghichu;
                command.Parameters.Add(columnTENMAYTINH, NpgsqlDbType.Text, 200).Value = this._stenmaytinh;
                command.Parameters.Add(columnCHIETKHAU, NpgsqlDbType.Numeric, 20).Value = this._dechietkhau;
                command.Parameters.Add(columnTONGTIEN, NpgsqlDbType.Numeric, 20).Value = this._detongtien;
                command.Parameters.Add(columnTIENKHACHDUA, NpgsqlDbType.Numeric, 20).Value = this._detienkhachdua;
                return (command.ExecuteNonQuery() != 0);
            }
            catch (NpgsqlException exception)
            {
                ConnectDatabase.TrackingError(exception.ToString(), "Update()", "dtoDXuatBanLe.cs");
                return false;
            }
        }

        public static string columnCHIETKHAU
        {
            get
            {
                return "chietkhau";
            }
        }

        public static string columnDIACHI
        {
            get
            {
                return "diachi";
            }
        }

        public static string columnGHICHU
        {
            get
            {
                return "ghichu";
            }
        }

        public static string columnHOTEN
        {
            get
            {
                return "hoten";
            }
        }

        public static string columnID
        {
            get
            {
                return "id";
            }
        }

        public static string columnIDDANGNHAP
        {
            get
            {
                return "iddangnhap";
            }
        }

        public static string columnIDGIOITINH
        {
            get
            {
                return "idgioitinh";
            }
        }

        public static string columnIDLOAIXUAT
        {
            get
            {
                return "idloaixuat";
            }
        }

        public static string columnIDNHOMKHACHHANG
        {
            get
            {
                return "idnhomkh";
            }
        }

        public static string columnIDTTVP
        {
            get
            {
                return "idttvp";
            }
        }

        public static string columnKIHIEU
        {
            get
            {
                return "kihieu";
            }
        }

        public static string columnNAMSINH
        {
            get
            {
                return "namsinh";
            }
        }

        public static string columnNGAY
        {
            get
            {
                return "ngay";
            }
        }

        public static string columnNGAYCAPNHAT
        {
            get
            {
                return "ngaycapnhat";
            }
        }

        public static string columnSOLANIN
        {
            get
            {
                return "solanin";
            }
        }

        public static string columnSOPHIEU
        {
            get
            {
                return "sophieu";
            }
        }

        public static string columnTENMAYTINH
        {
            get
            {
                return "tenmaytinh";
            }
        }

        public static string columnTIENKHACHDUA
        {
            get
            {
                return "tienkhachdua";
            }
        }

        public static string columnTONGTIEN
        {
            get
            {
                return "tongtien";
            }
        }

        private string NGAYCAPNHAT
        {
            get
            {
                return this._sngaycapnhat;
            }
            set
            {
                this._sngaycapnhat = value;
            }
        }

        public decimal pCHIETKHAU
        {
            get
            {
                return this._dechietkhau;
            }
            set
            {
                this._dechietkhau = value;
            }
        }

        public string pDIACHI
        {
            get
            {
                return this._sdiachi;
            }
            set
            {
                this._sdiachi = value;
            }
        }

        public string pGHICHU
        {
            get
            {
                return this._sghichu;
            }
            set
            {
                this._sghichu = value;
            }
        }

        public string pHOTEN
        {
            get
            {
                return this._shoten;
            }
            set
            {
                this._shoten = value;
            }
        }

        public long pID
        {
            get
            {
                return this._deid;
            }
            set
            {
                this._deid = value;
            }
        }

        public int pIDDANGNHAP
        {
            get
            {
                return this._deiddangnhap;
            }
            set
            {
                this._deiddangnhap = value;
            }
        }

        public int pIDGIOITINH
        {
            get
            {
                return this._iidgioitinh;
            }
            set
            {
                this._iidgioitinh = value;
            }
        }

        public int pIDLOAIXUAT
        {
            get
            {
                return this._iidloaixuat;
            }
            set
            {
                this._iidloaixuat = value;
            }
        }

        public int pIDNHOMKHACHHANG
        {
            get
            {
                return this._iidnhomkhachhang;
            }
            set
            {
                this._iidnhomkhachhang = value;
            }
        }

        public long pIDTTVP
        {
            get
            {
                return this._deidttvp;
            }
            set
            {
                this._deidttvp = value;
            }
        }

        public string pKIHIEU
        {
            get
            {
                return this._skihieu;
            }
            set
            {
                this._skihieu = value;
            }
        }

        public string pNAMSINH
        {
            get
            {
                return this._snamsinh;
            }
            set
            {
                this._snamsinh = value;
            }
        }

        public string pNGAY
        {
            get
            {
                return this._sngay;
            }
            set
            {
                this._sngay = value;
            }
        }

        public int pSOLANIN
        {
            get
            {
                return this._isolanin;
            }
            set
            {
                this._isolanin = value;
            }
        }

        public long pSOPHIEU
        {
            get
            {
                return this._desophieu;
            }
            set
            {
                this._desophieu = value;
            }
        }

        public string pTENMAYTINH
        {
            get
            {
                return this._stenmaytinh;
            }
            set
            {
                this._stenmaytinh = value;
            }
        }

        public decimal pTIENKHACHDUA
        {
            get
            {
                return this._detienkhachdua;
            }
            set
            {
                this._detienkhachdua = value;
            }
        }

        public decimal pTONGTIEN
        {
            get
            {
                return this._detongtien;
            }
            set
            {
                this._detongtien = value;
            }
        }

        public static string tableName
        {
            get
            {
                return "d_xuatkho_banle";
            }
        }
    }
}

